package pe.edu.unmsm.fisi.sisconteg.repository.dao.impl;

import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Repository;

import pe.edu.unmsm.fisi.sisconteg.domain.model.Tesis;
import pe.edu.unmsm.fisi.sisconteg.repository.dao.TesisDAO;

@Repository
public class TesisDAOImpl implements TesisDAO {

	@Autowired
	private SessionFactory sessionFactory;
	
	@Override
	public void crearTesis(Tesis tesis) throws DataAccessException {
		sessionFactory.getCurrentSession().persist(tesis);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tesis> getTesisxTema(String temaTesis)
			throws DataAccessException {
	//	return sessionFactory.getCurrentSession().createCriteria(Tesis.class).add(Restrictions.like("titulo", temaTesis)).list();
		String s = "select tes from Tesis tes left join fetch tes.egresado egre where tes.titulo like ?";
		return sessionFactory.getCurrentSession().createQuery(s).setString(0, "%" + temaTesis + "%").list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tesis> getTesisxFlag(Boolean tipoTesisTesina)
			throws DataAccessException {
		//return sessionFactory.getCurrentSession().createCriteria(Tesis.class).add(Restrictions.eq("flagTesisTesina", tipoTesisTesina)).list();
		String s = "select tes from Tesis tes left join fetch tes.egresado egre where tes.flagTesisTesina = :tipoTes";
		return sessionFactory.getCurrentSession().createQuery(s).setParameter("tipoTes", tipoTesisTesina).list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<Tesis> getTesisxGrado(Integer gradoTesis)
			throws DataAccessException {
		String s = "select tes from Tesis tes left join fetch tes.egresado where tes.gradoTesis.idGradoTesis = :gradoTes";
		return sessionFactory.getCurrentSession().createQuery(s)
				.setParameter("gradoTes", gradoTesis).list();
	}

}
